// pages/notice/list.js
var app = getApp();
var api = require('../../api.js'),
page_size=15;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 15,
    records: [],
    is_edit: false,
  },
   // 获取关键词
  get_keyword: function (e) {
    this.setData({
      keywords: e.detail.value
    })
    this.fn_search();
  },
  // 搜索
  fn_search: function () {
    var keywoerds = this.data.keywords,
      department_id = this.data.department_id;
    this.setData({
      list: []
    })
    this.get_data(1, page_size, keywoerds)
  },
 
  // 编辑通话收藏
  go_edit: function (e) {
    this.setData({
      is_edit: true
    })

  },
  // 返回查看
  go_no_edit: function () {
    this.setData({
      is_edit: false
    })
  },
  // 删除通话收藏
  go_delete: function () {
    var that=this;
    var records = that.data.records;
    var id_list=[];
    for(var i=0; i<records.length;i++){
      id_list.push(records[i].id)
    }
    if (records.length < 1) {
      wx.showToast({
        title: '请选择要删除的收藏！',
        icon: 'none',
      })
    } else {
      wx.showModal({
        title: '我的收藏删除提示',
        content: '确定删除？',
        showCancel: true,
        success: (res) => {
          app.ajax(api.delete_collection, {
            rd_session: wx.getStorageSync('rd_session'),
            id_list: id_list,
          }, (res, err) => {
            wx.showToast({
              title: '删除成功！',
              icon: 'none',
              duration: 200,
              mask: true,
              success: (res)=> {
                that.get_data(1, page_size, that.data.keywoerds)
              },
            })
            
          })
      
        },
      })

    }
  },
  // 关闭弹窗
  close_pop: function () {
    if (this.data.is_pop) {
      var list = this.data.list;
      list[this.data.list_index].is_choose = false;
      this.setData({
        is_pop: false,
        tels: [],
        records: [],
        record_tels: [],
        list: list
      })
    } else {
      this.setData({
        is_pop: true
      })
    }
  },

  // 选择要删除的收藏
  fn_choose: function (e) {
    var id = e.currentTarget.dataset.id, is_choose = e.currentTarget.dataset.is_choose,
      index = e.currentTarget.dataset.index,
      list = this.data.list,
      records = this.data.records;
    this.setData({
      list_index: index
    })
    if (is_choose == false) {
      list[index].is_choose = true;
      records.push(list[index]);
      this.setData({
        list: list,
        records: records,
      })
      this.setData({
        is_pop: true,
      })
    } else {
      for (var i = 0; i < records.length; i++) {
        if (records[i].id == id) {
          records.splice(i, 1)
        }
      }
      list[index].is_choose = false;
      this.setData({
        list: list,
        records: records
      })
    }
  },

  // 产看详情
  go_detail: function (e) {
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/leader/detail?id=' + id,
    })
  },
  // 获取数据
  get_data: function (page,page_size,name) {
    app.ajax(api.my_collection, {
      rd_session: wx.getStorageSync('rd_session'),
      page: 1,
      page_size: page_size,
      name: name,
    }, (res, err) => {
      this.setData({
        list: res.data.list,
        total_page: res.total_page,
        page: 1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      member_type: wx.getStorageInfoSync("member_type"),
    })
 this.get_data(1,page_size,"")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_data(1, page_size, "")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var total_page = this.data.total_page;
    var page = this.data.page;
    if (page >= total_page) {
      wx.showToast({
        title: '暂无更多~',
        icon: 'none'
      })
    } else {
      page++;
      app.ajax(api.my_collection, {
        rd_session: wx.getStorageSync('rd_session'),
        page: page,
        page_size: page_size,
        name: this.data.keywords
      }, (res, err) => {
        var arr1 = this.data.list;
        var arr2 = res.data.list;
        var arr3 = arr1.concat(arr2);
        this.setData({
          total_page: res.total_page,
          list: arr3,
          page: page
        })
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})